package com.huawei.appgallery.forum.cards.widget;

import android.content.Context;
import android.text.Layout.Alignment;
import android.text.SpannableStringBuilder;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;
import o.bqx;
import o.bsw;
import o.ContentManager;

public class FoldTextView extends TextView {
    private boolean ʻ = false;
    private e ˊ;
    private CharSequence ˋ;
    private int ˎ = 3;
    private SpannableStringBuilder ˏ;
    private CharSequence ॱ;
    private boolean ॱॱ = false;

    public interface e {
        void ˎ(boolean z);
    }

    public FoldTextView() {
        super(ContentManager.getInstant().getContext());
    }

    public FoldTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public e ˏ() {
        return this.ˊ;
    }

    public void setListener(e eVar) {
        this.ˊ = eVar;
    }

    public void ˎ() {
        if (this.ॱ != null && this.ˋ != null) {
            if (this.ॱ.toString().equals(this.ˋ.toString())) {
                this.ॱ = this.ˏ;
            } else {
                this.ॱ = this.ˋ;
            }
            this.ॱॱ = !this.ॱॱ;
            setText(this.ॱ);
        }
    }

    private SpannableStringBuilder ˎ(SpannableStringBuilder spannableStringBuilder, int i, TextPaint textPaint) {
        float measureText = textPaint.measureText(spannableStringBuilder.toString());
        float measureText2 = textPaint.measureText("...");
        if (((float) i) < measureText + measureText2) {
            do {
                spannableStringBuilder.delete(spannableStringBuilder.length() - 2, spannableStringBuilder.length() - 1);
            } while (((float) i) < textPaint.measureText(spannableStringBuilder.toString()) + measureText2);
        }
        return spannableStringBuilder;
    }

    public void setOnContentChangedListener(e eVar) {
        setListener(eVar);
    }

    public void setContent(SpannableStringBuilder spannableStringBuilder, boolean z, int i) {
        this.ˏ = spannableStringBuilder;
        this.ॱॱ = z;
        this.ॱ = null;
        this.ˋ = null;
        this.ʻ = false;
        try {
            ˏ(i);
        } catch (Exception e) {
            bqx.ˋ("MultiLineTextView", "resizeMeasure error:" + e.toString());
        }
    }

    public void setMaxLine(int i) {
        this.ˎ = i;
    }

    private void ˏ(int i) {
        int ˊ = ˊ(i);
        if (TextUtils.isEmpty(this.ˏ) || i <= 0) {
            setText("");
            this.ˋ = this.ˏ;
        } else {
            CharSequence append;
            StaticLayout staticLayout = new StaticLayout(this.ˏ, getPaint(), ˊ, Alignment.ALIGN_NORMAL, getLineSpacingMultiplier(), getLineSpacingExtra(), false);
            int lineCount = staticLayout.getLineCount();
            TextPaint textPaint = new TextPaint();
            textPaint.setTextSize(getTextSize());
            if (lineCount > this.ˎ) {
                this.ʻ = true;
                CharSequence ॱ = bsw.ॱ(this.ˎ, staticLayout, this.ˏ);
                SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
                spannableStringBuilder.append(ॱ);
                append = new SpannableStringBuilder().append(this.ˏ.subSequence(0, (bsw.ˎ(this.ˎ - 1, staticLayout, this.ˏ).length() + ˎ(spannableStringBuilder, ˊ, textPaint).length()) - 1)).append("...");
            } else {
                append = this.ˏ;
            }
            this.ˋ = append;
        }
        if (this.ॱॱ) {
            this.ॱ = this.ˏ;
            setText(this.ˏ);
        } else {
            this.ॱ = this.ˋ;
            setText(this.ˋ);
        }
        if (ˏ() != null) {
            ˏ().ˎ(this.ʻ);
        }
    }

    private int ˊ(int i) {
        return (i - getPaddingStart()) - getPaddingEnd();
    }
}
